package org.example.spring.controller.User;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.example.spring.context.BaseContext;
import org.example.spring.domain.dto.CartDTO;
import org.example.spring.domain.pojo.Cart;
import org.example.spring.domain.pojo.Result;
import org.example.spring.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

@RestController("userCartController")
@Api(tags = "购物车相关接口")
@RequestMapping("/user/cart")
public class CartController {

    @Autowired
    private CartService cartService;

    @ApiOperation("添加购物车")
    @PostMapping("/addCart")
    public Result addCart(@RequestBody Cart cart) {
        cartService.addCart(cart);
        return Result.success("添加成功");
    }

    @ApiOperation("得到购物车信息")
    @GetMapping("/getCartList")
    public Result<ArrayList<CartDTO>> getCart() {
        ArrayList<CartDTO> cartList = cartService.getCartList();
        return Result.success(cartList);
    }

    @ApiOperation("删除购物车")
    @DeleteMapping("/deleteCart")
    public Result deleteCart(@RequestParam Integer flowerId) {
        Integer userId = BaseContext.getCurrentId();
        cartService.deleteCart(flowerId, userId);
        return Result.success("删除成功");
    }

    @ApiOperation("更新购物车")
    @PutMapping("/updateCart")
    public Result updateCart(@RequestParam Integer flowerId, @RequestParam Integer quantity) {
        Integer userId = BaseContext.getCurrentId();
        cartService.addCart(userId, flowerId, quantity);
        return Result.success("更新成功");
    }

}
